package com.sqhe.gamedev.core
{       
	import as3isolib.core.IsoContainer;
	import as3isolib.display.IsoSprite;
	import as3isolib.display.IsoView;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.scene.IsoGrid;
	import as3isolib.display.scene.IsoScene;
	import as3isolib.geom.IsoMath;
	import as3isolib.geom.Pt;
	
	import com.bit101.components.PushButton;
	import com.sqhe.gamedev.core.configs.IsoGridConfig;
	import com.sqhe.gamedev.core.configs.IsoSpriteConfig;
	import com.sqhe.gamedev.core.pathFind.AStar;
	import com.sqhe.gamedev.core.pathFind.Grid;
	import com.sqhe.gamedev.core.pathFind.Node;
	import com.sqhe.gamedev.core.resources.SceneResources;
	import com.sqhe.gamedev.core.resources.WalkArray;
	import com.sqhe.gamedev.core.resources.WalkResources;
	import com.sqhe.gamedev.core.scene.CustomerSprite;
	import com.sqhe.gamedev.core.skin.CustomerStroke;
	
	import eDpLib.events.ProxyEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import gs.TweenLite;
	import gs.easing.Linear;
	
	public class MainView extends Sprite
	{
		//显示
		private var isoGrid:IsoGrid;
		private var isoScene:IsoScene;
		private var isoView:IsoView;
		
		//pathfind
		private var _grid:Grid;
		
		
		private var timeSpan:Number=1;			//人物移动一格所需要的时间,单位秒
		
		private var stageWidth:Number=800;
		private var stageHeight:Number=535;
		
		private var eventType:String='move';
		
		private var astar:AStar;
		
		private var defaultWalkType:String=WalkArray.DOWN;
		private var walkArray:Array;
		
		public function MainView ()
		{
			isoGrid = new IsoGrid();
			isoGrid.stroke=new CustomerStroke(IsoGridConfig.thickness,IsoGridConfig.color);
			isoGrid.showOrigin = false;
			isoGrid.setGridSize(IsoGridConfig.sizeRows, IsoGridConfig.sizeCols);
			isoGrid.cellSize=IsoGridConfig.cellSize;
			isoGrid.moveBy(IsoGridConfig.fixX,IsoGridConfig.fixY,IsoGridConfig.fixZ);
			
			isoScene = new IsoScene();
			isoScene.addChild(isoGrid);
			
			/*sprite = new IsoSprite();
			sprite.sprites=[WalkResources['walk'+defaultWalkType]];
			moveToFix(sprite,0,0,0);
			isoScene.addChild(sprite);*/
			isoView= new IsoView();
			isoView.setSize(stageWidth, stageHeight);
			isoView.showBorder=false;
			isoView.addScene(isoScene); 
			addChild(isoView);
			pathFinderInit();
			
			var deskSprite:IsoSprite = new IsoSprite();
			deskSprite.sprites = [SceneResources['desk01']];
			moveToFix(deskSprite,5,5,0);
			setWalkableByRange(6,3,4,4);
			isoScene.addChild(deskSprite);
			
			var deskSprite3:IsoSprite = new IsoSprite();
			deskSprite3.sprites = [SceneResources['desk01']];
			moveToFix(deskSprite3,10,5,0);
			setWalkableByRange(11,3,4,4);
			isoScene.addChild(deskSprite3);
			
			var deskSprite2:IsoSprite = new IsoSprite();
			deskSprite2.sprites = [SceneResources['desk02']];
			moveToFix(deskSprite2,0,4,0);
			setWalkableByRange(2,3,4,1);
			isoScene.addChild(deskSprite2);
			
			
			var deskSprite4:IsoSprite = new IsoSprite();
			deskSprite4.sprites = [SceneResources['desk03']];
			moveToFix(deskSprite4,4,0,0);
			setWalkableByRange(5,0,8,2);
			isoScene.addChild(deskSprite4);
			
			
			var zhaozuSprite:IsoSprite = new IsoSprite();
			zhaozuSprite.sprites = [SceneResources['zhaozu']];
			moveToFix(zhaozuSprite,6,11,0);
			setWalkableByRange(7,9,8,5);
			isoScene.addChild(zhaozuSprite);
			
			
			
			
			isoScene.render();
			
			addEventListener(Event.ENTER_FRAME, enterFrame); 
			randomPoepleWork(10);
		}
		
		public function randomPoepleWork(peopleNum:int):void{
			var i:int=0;
			var ilength:int=peopleNum;
			for(i;i<ilength;i++){
				var sprite:CustomerSprite = new CustomerSprite();
				sprite.sprites=[WalkResources['walk'+defaultWalkType]];
				
				var xpos:int=int(Math.random()*IsoGridConfig.sizeRows);
				var ypos:int=int(Math.random()*IsoGridConfig.sizeCols);
				while(!_grid.getNode(xpos,ypos).walkable){
					xpos=int(Math.random()*IsoGridConfig.sizeRows);
					ypos=int(Math.random()*IsoGridConfig.sizeCols);
				}
				moveToFix(sprite,xpos,ypos,0);
				isoScene.addChild(sprite);
				_grid.setStartNode(xpos,ypos);
				trace(sprite.id,'start:'+xpos,ypos)
				var targetXpos:int=int(Math.random()*IsoGridConfig.sizeRows);
				var targetYpos:int=int(Math.random()*IsoGridConfig.sizeCols);
				while(!_grid.getNode(targetXpos,targetYpos).walkable){
					targetXpos=int(Math.random()*IsoGridConfig.sizeRows);
					targetYpos=int(Math.random()*IsoGridConfig.sizeCols);
				}
				_grid.setEndNode(targetXpos,targetYpos);
				trace(sprite.id,'end:'+targetXpos,targetYpos)
				if(astar.findPath(_grid))
				{
					trace(_grid.startNode.x,_grid.startNode.y,_grid.endNode.x,_grid.endNode.y);
					sprite.path=astar.path;
					moveCal(sprite);
				}
			}
			isoScene.render();
		}
		
		private function setWalkableByRange(start:int,end:int,width:int,height:int):void{
			var i:int=start;
			var ilength:int=start+width;
			for(i;i<ilength;i++){
				trace(i);
				var j:int=end;
				var jlength:int=end+height;
				for(j;j<jlength;j++){
					setWalkable(i,j,false);
					trace(i,j);
				}
			}
		}
		
		private function setWalkable(xpos:int,ypos:int,tf:Boolean):void{
			if(_grid.getNode(xpos,ypos).walkable){
				_grid.getNode(xpos,ypos).walkable=tf;
			}
		}
		
		private function pathFinderInit():void{
			_grid = new Grid(IsoGridConfig.sizeRows, IsoGridConfig.sizeCols);
			_grid.setStartNode(0, 0);
			_grid.setEndNode(0, 0);
			
			if(!astar){
				astar= new AStar();
			}
		}

		private function clearBoxFunc():void{
			var i:int=isoScene.children.length-1;
			for(i; i>0;i--){
				if(isoScene.children[i] is IsoBox){
					isoScene.removeChild(isoScene.children[i]);
				}
			}
		}
		
		
		private function moveCal(sprite:CustomerSprite):void{
			if(!sprite)return;
			if(!walkArray){
				walkArray=new Array;
			}else{
				walkArray.length=0;
			}
			var array:Array=sprite.path;
			var tempWalkType:String='';
			var waLength:int=0;
			
			var i:int=0;
			var ilength:int=array.length;
			for(i;i<ilength;i++){
				var node:Node=array[i];
				
				if(waLength==0){
					waLength++;
				}else{
					if(tempWalkType==''){
						tempWalkType=getWalkTypeByNodes(node,sprite.preNode);
						waLength++;
					}
					else{
						if(tempWalkType!=getWalkTypeByNodes(node,sprite.preNode))
						{
							break;
						}else{
							waLength++;
						}
					}
				}
				sprite.preNode=node;
			}
			
			walkArray=array.splice(0,waLength);
			if(tempWalkType!=''){
				sprite.sprites=[WalkResources['walk'+tempWalkType]];
				sprite.render();
			}
			else{
				moveCompleteFunc(sprite);
			}
			if(walkArray.length>0){
				var lastNode:*=walkArray[walkArray.length-1];
				TweenLite.to(sprite, timeSpan*walkArray.length, 
					{x: walkArray[walkArray.length-1].x*IsoSpriteConfig.size+IsoSpriteConfig.xFix+IsoGridConfig.fixX, 
						y:walkArray[walkArray.length-1].y*IsoSpriteConfig.size + IsoSpriteConfig.yFix+IsoGridConfig.fixY,
						ease:Linear.easeNone,
						onComplete:moveCompleteFunc,
						onCompleteParams:[sprite]});
				if(astar.path.length>0){
					array.unshift(lastNode);
				}
			}
			
		}
		
		private function moveCompleteFunc(sprite:CustomerSprite):void{
			if(sprite.path.length>0){
				moveCal(sprite);
			}
			else{
				sprite.preNode=null;
				var p:Point=getSpritePoint(sprite);
				_grid.setStartNode(p.x,p.y);
				trace(sprite.id,'start:'+p.x,p.y)
				var targetXpos:int=int(Math.random()*IsoGridConfig.sizeRows);
				var targetYpos:int=int(Math.random()*IsoGridConfig.sizeCols);
				while(!_grid.getNode(targetXpos,targetYpos).walkable){
					targetXpos=int(Math.random()*IsoGridConfig.sizeRows);
					targetYpos=int(Math.random()*IsoGridConfig.sizeCols);
				}
				_grid.setEndNode(targetXpos,targetYpos);
				trace(sprite.id,'end:'+targetXpos,targetYpos)
				if(astar.findPath(_grid))
				{
					trace(_grid.startNode.x,_grid.startNode.y,_grid.endNode.x,_grid.endNode.y);
					sprite.path=astar.path;
					moveCal(sprite);
				}
			}
		}
		
		private function getWalkTypeByNodes(node:Node,preNode:Node):String{
			var tempWalkType:String;
			switch(true){
				case (node.x==preNode.x && (node.y>preNode.y)):{
					tempWalkType=WalkArray.LEFT;
					break;
				}
				case (node.x==preNode.x && (node.y<preNode.y)):{
					tempWalkType=WalkArray.RIGHT;
					break;
				}
				case (node.y==preNode.y && (node.x>preNode.x)):{
					tempWalkType=WalkArray.DOWN;
					break;
				}
				case (node.y==preNode.y && (node.x<preNode.x)):{
					tempWalkType=WalkArray.UP;
					break;
				}
			}
			return tempWalkType;
		}
		
		private function moveToFix(_sprite:IsoSprite,_x:Number,_y:Number,_z:Number):void{
			_sprite.moveTo(_x*IsoSpriteConfig.size+IsoSpriteConfig.xFix+IsoGridConfig.fixX,
				_y*IsoSpriteConfig.size+IsoSpriteConfig.yFix+IsoGridConfig.fixY,
				_z*IsoSpriteConfig.size+IsoSpriteConfig.zFix);
		}
		
		private function getSpritePoint(sprite:IsoSprite):Point{
			var point:Point=new Point();
			point.x=(sprite.x-(IsoSpriteConfig.xFix+IsoGridConfig.fixX))/IsoSpriteConfig.size;
			point.y=(sprite.y-(IsoSpriteConfig.yFix+IsoGridConfig.fixY))/IsoSpriteConfig.size;	
			return point;
		}
		
		private function intFix(nun:Number,fixNum:Number=0):Number{
			return(Math.floor(nun/IsoSpriteConfig.size)*IsoSpriteConfig.size+fixNum);
		}
		
		protected function enterFrame(event:Event):void
		{
			isoScene.render();
		}
		
	}
}